const axios = require('axios');
const qs = require('querystring');

/**
 * @author ycx
 * 请求工具类
 */
class RequestUtil {

    /**
     *
     * @param url {string}
     * @param params {Object}
     * @return {Promise<*>}
     */
    static async get(url, params) {

        url = -1 !== url.indexOf('?') ? url + '&' + qs.stringify(params) : url + '?' + qs.stringify(params);

        const axiosResponse = await axios.get(url, {});
        return axiosResponse.data;
    }

    /**
     * accessToken 放data中
     * @param url
     * @param data
     * @return {Promise<*>}
     */
    static async post(url, data) {

        url = -1 !== url.indexOf('?') ? url + '&' + data.accessToken : url + '?' + data.accessToken;
        //
        delete data.accessToken;
        //
        const axiosResponse = await axios.post(url, data);
        return axiosResponse.data;

    }
}

module.exports = RequestUtil;
